package com.tqh;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.*;

import cal.Environments;
import cal.Road;
import cal.Truck;
import org.json.JSONArray;


//@WebServlet("MyServlet")
public class MyServlet extends HttpServlet {

    static{
        System.load("D://项目//MyServlet//dll_test.dll");
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        doGet(request,response);
        //从前端传递的request中取值
        String[] roads=request.getParameterValues("roads[]");
        String[] polyline=request.getParameterValues("polyline[]");
        String[] instruction=request.getParameterValues("instruction[]");

        String[] stepPolyline=request.getParameterValues("step_path[]");


        String[] getInfo=HttpPretreatment.pretreatment_Instruction(instruction,stepPolyline);
//      String[] roadsss={"","南焦路","顺平路","回民营桥"};
//      Road[] RS=HttpPretreatment.pretreatment_Road(roadsss);
        Road[] RS=HttpPretreatment.pretreatment_Road(roads,stepPolyline);
        Truck truck=new Truck();
        Environments env=new Environments();
        String[] polyline_de=HttpPretreatment.pretreatment_Polyline(polyline);

        String[] sss1=GetDll.checkPass1(truck, RS, env);
        String[] sss2=GetDll.checkPass2(truck, RS, env,polyline_de);
        String[] sss3=GetDll.checkPass3(truck, RS, env,polyline_de);
        System.out.println(polyline.length/2+"|||"+polyline_de.length);
        String[] ss=HttpPretreatment.de_check1(sss1[2],stepPolyline);
        System.out.println("前端传递过来的名字是" + roads[2]);

        JSONArray name=new JSONArray(ss);
        JSONArray jsonArray=new JSONArray(sss2);
        JSONArray jsonArray1=new JSONArray(sss3);
        JSONArray jsonArray2=new JSONArray(getInfo);
        JSONArray jsonArray3=new JSONArray(polyline_de);
        //构造json字符串传递回前端，\为java转义符号为了转义双引号
        //注意这里格式要求很严格，不能用单引号，建议使用第三方框架自动生成json字符串
//        String s = sss[0]+"|||||"+sss[2];
        String s="{\"name\":\""+sss1[2]+"\""+
                ",\"check1\":"+name.toString()+
                ",\"check2\":"+jsonArray.toString()+
                ",\"check3\":"+jsonArray1.toString()+
                ",\"check4\":"+jsonArray2.toString()+
                ",\"poly\":"+jsonArray3.toString()+"}";
        response.setCharacterEncoding("utf-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Allow-Credentials", "true");

        response.setContentType("application/json; charset=utf-8");//返回的格式必须设置为application/json
        response.getWriter().write(s);//写入到返回结果中
        //完成，执行到这里就会返回数据给前端，前端结果为success，调用success里面的内容
        //response.setHeader("Access-Control-Allow-Origin", "*");
        //解决跨越问题
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        out.println("this is servlet");


        String[] roads=request.getParameterValues("roads[]");
        String[] polyline=request.getParameterValues("polyline[]");
        String[] instruction=request.getParameterValues("instruction[]");

        String[] stepPolyline=request.getParameterValues("step_path[]");


        String[] getInfo=HttpPretreatment.pretreatment_Instruction(instruction,stepPolyline);
//      String[] roadsss={"","南焦路","顺平路","回民营桥"};
//      Road[] RS=HttpPretreatment.pretreatment_Road(roadsss);
        Road[] RS=HttpPretreatment.pretreatment_Road(roads,stepPolyline);
        Truck truck=new Truck();
        Environments env=new Environments();
        String[] polyline_de=HttpPretreatment.pretreatment_Polyline(polyline);

        String[] sss1=GetDll.checkPass1(truck, RS, env);
        String[] sss2=GetDll.checkPass2(truck, RS, env,polyline_de);
        String[] sss3=GetDll.checkPass3(truck, RS, env,polyline_de);
        System.out.println(polyline.length/2+"|||"+polyline_de.length);

        System.out.println("前端传递过来的名字是" + roads[2]);

        JSONArray jsonArray=new JSONArray(sss2);
        JSONArray jsonArray1=new JSONArray(sss3);
        JSONArray jsonArray2=new JSONArray(getInfo);
        JSONArray jsonArray3=new JSONArray(polyline_de);
        //构造json字符串传递回前端，\为java转义符号为了转义双引号
        //注意这里格式要求很严格，不能用单引号，建议使用第三方框架自动生成json字符串

        String s="{\"name\":\""+sss1[2]+
                "\",\"check2\":"+jsonArray.toString()+
                ",\"check3\":"+jsonArray1.toString()+
                ",\"check4\":"+jsonArray2.toString()+
                ",\"poly\":"+jsonArray3.toString()+"}";

//        //前端传过来的回调函数名称
//        String callback = request.getParameter("callback");
//        //用回调函数名称包裹返回数据，这样，返回数据就作为回调函数的参数传回去了
//        String s = callback + "(" + jsonArray3.toString() + ")";

        response.setCharacterEncoding("utf-8");
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Allow-Credentials", "true");

        response.setContentType("application/json; charset=utf-8");//返回的格式必须设置为application/json
        response.getWriter().write(s);//写入到返回结果中
        //完成，执行到这里就会返回数据给前端，前端结果为success，调用success里面的内容
        //response.setHeader("Access-Control-Allow-Origin", "*");
        //解决跨越问题
    }
}